package com.yxhl.zoume.wxapi;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.yxhl.zoume.R;
import com.yxhl.zoume.ZMConfig;
import com.yxhl.zoume.common.ui.activity.base.BaseActivity;
import com.yxhl.zoume.core.car.model.PayResultEvent;
import com.yxhl.zoume.core.car.model.StatusEnum;
import com.yxhl.zoume.core.func.pay.model.event.PaymentSuccessfulEvent;
import com.yxhl.zoume.core.main.info.MainEntrance;
import com.yxhl.zoume.core.main.model.MainEntranceInfo;
import com.yxhl.zoume.core.user.ui.info.WeChatResultEvent;
import com.yxhl.zoume.data.rxbus.RxBus;
import com.yxhl.zoume.utils.WeChatPayEntryUtils;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * Created by vhawk on 16/1/27.
 */
public class WXPayEntryActivity extends BaseActivity implements IWXAPIEventHandler {
    private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";
    @BindView(R.id.payinfo_textview)
    TextView mPayInfoTextView;
    @BindView(R.id.confirm_textview)
    TextView mConfirmTextView;
    private IWXAPI api;
    private boolean mIsRecharge;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, ZMConfig.WX_APPID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {
        mIsRecharge = WeChatPayEntryUtils.getInstance(this).getWechatRechargeEntry();
        if (resp.errCode == 0) {

            if (mIsRecharge){
                RxBus.getInstance().send(new WeChatResultEvent(true));
                mPayInfoTextView.setText("钱包充值成功!");

            }else {
                mPayInfoTextView.setText("订单支付成功!");
                RxBus.getInstance().send(new PayResultEvent(StatusEnum.STATUS_ENUM_TRUE));
            }

        } else {
            if (mIsRecharge){
                RxBus.getInstance().send(new WeChatResultEvent(false));
                mPayInfoTextView.setText("钱包充值失败!");

            }else{
                mPayInfoTextView.setText("订单支付失败!");
                RxBus.getInstance().send(new PayResultEvent(StatusEnum.STATUS_ENUM_FALSE));
            }

        }
        WeChatPayEntryUtils.getInstance(this).saveWechatRechargeEntry(false);
    }

    @OnClick(R.id.confirm_textview)
    public void onConfirmClick() {
        if (mIsRecharge){
            finish();
        }else {
            RxBus.getInstance().send(new PaymentSuccessfulEvent());
            getAppComponent().navigator().navigateToMain(this, new MainEntranceInfo(MainEntrance.PAYMENT));
        }
    }

    @Override
    public void onBackPressed() {
        if (mIsRecharge){
            finish();
        }else {
            getAppComponent().navigator().navigateToMain(this, new MainEntranceInfo(MainEntrance.PAYMENT));
        }
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_wechat_pay_result;
    }


    @Override
    protected void initializeInjector() {
    }
}
